<template>
  <div class="card" style="width: 50%">
    <el-form title="个人信息"   ref="formRef" :rules="data.rules" :model="data.form" style="padding-right: 40px;padding-top: 20px" >

        <div style="width: 100%;display: flex;justify-content: center;margin-bottom: 20px">
          <el-upload
              class="avatar-uploader"
              :action= "$baseUrl + '/files/upload'"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
          >
            <img v-if="data.form.avatar" :src="data.form.avatar" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </div>


      <el-form-item label="账号" prop="username">
        <el-input v-model="data.form.username" autocomplete="off" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="data.form.name" autocomplete="off"  placeholder="请输入名称"></el-input>
      </el-form-item>
      <div v-if="data.user.role === 'EMP'">
        <el-form-item label="性别" >
          <el-radio-group v-model="data.form.gender" id="gender-radio-group">
            <el-radio value="男" size="large" border>男</el-radio>
            <el-radio value="女" size="large" border>女</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="职称">
          <el-input v-model="data.form.title" autocomplete="off"  placeholder="请输入职称"></el-input>
        </el-form-item>

        <el-form-item label="出生日期">

          <el-date-picker
              v-model="data.form.birthday"
              type="date"
              placeholder="选择出生日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </div>

      <el-form-item>
        <el-button type="primary" @click="update">更新</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>


  </div>
</template>
<script setup>
import {ref, reactive} from 'vue'
import request from "@/utils/request.js";
import {ElMessage} from 'element-plus'
const formRef = ref()

const data = reactive({

  rules: {
    name:[
      {required:true,message:'请输入名称',trigger:'blur'}
    ]
  },

  form:{
    username:'',
    password:'',
    role:'',
    name:'',
    gender:'',
    title:'',
    birthday:'',
    avatar:'',
  },
  user: JSON.parse(localStorage.getItem("zwy-user"))
})
data.form = data.user
const emit =  defineEmits(['update'])
const update = () => {
   const validate = formRef.value.validate
  if(validate) {
    if(data.user.role==='EMP'){
    request.put('/employee/modify', data.form).then(res =>{
      if(res.code === '200'){
        ElMessage.success('更新成功');
        localStorage.setItem('zwy-user',JSON.stringify(data.form));
        // 通知父组件更新数据(定义一个事件)
        emit('update')

      }else {
        ElMessage.error(res.code + '/n' + res.msg);
      }
    })
  }
    if(data.user.role==='ADMIN'){
      request.put('/admin/modify', data.form).then(res =>{
        if(res.code === '200'){
          ElMessage.success('更新成功');
          localStorage.setItem('zwy-user',JSON.stringify(data.form));
          emit('update')
        }else {
          ElMessage.error(res.code + '/n' + res.msg);
        }
      })
    }
  }

}
const reset = () => {
  location.reload();
}

const handleAvatarSuccess = (res) => {
   console.log(res.data)
  data.form.avatar = res.data.url

}

</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block; 
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  text-align: center;
}
</style>